'''
Created on 24.10.2012

@author: fixje
'''
import unittest
from DataModel import KnowteDao, Knowte


class KnowteDaoTest(unittest.TestCase):


    def setUp(self):
        self.kdao = KnowteDao()
        self.connection = self.kdao.conn
        self.clearDb()
        self.addData()
        
    def clearDb(self):
        self.connection.execute('delete from has_tag;')
        self.connection.execute('delete from tags;')
        self.connection.execute('delete from knowtes;')
        

    def tearDown(self):
        pass #self.clearDb()
        
    def addData(self):
        knowtes = []
        for i in range(100):
            k = Knowte()
            if i % 2 == 0: 
                t = "note"
            else:
                t = "bookmark"
                k.url = "http://url%d.com/item" % i
            k.title = "Title for a new %s with number %d" % (t, i)
            k.info = "Awesome description for a knowte with number %d." % i
            k.setTags([ "tag%d" % i, "%sTag" % t, "sharedTag"])
            knowtes.append(k)
        self.kdao.storeKnowtes(knowtes)
        
    def testInsert(self):
        assert self.kdao.getBookmarkCount() == 50, "bookmark count 50"
        assert self.kdao.getNoteCount() == 50, "note count 50"
        for k in self.kdao.getKnowtes():
            assert len(k.taglist()) > 0, "Every knowte must have a tag"
        
    def testEditKnowte(self):
        k1 = self.kdao.getKnowteById(1)
        k2 = self.kdao.getKnowteById(2)
        
        k1.title = "Edited Title"
        k1.info = "Edited Info"
        k1.setTags(["edited1"])

        self.kdao.editKnowte(1, k1)
        k1 = self.kdao.getKnowteById(1)
        # knowte has been correctly edited
        assert k1.title == "Edited Title", "title 1 edited"
        assert k1.info == "Edited Info", "info 1 edited"
        assert len(k1.taglist()) == 1, "just 1 tag"
        assert k1.taglist()[0] == "edited1"
        # all other knowtes still have their values
        for k in self.kdao.getKnowtes():
            if k.id != 1:
                assert k.title != "Edited Title", "title not edited"
                assert k.info != "Edited Info", "info not edited"
                assert len(k.taglist()) > 1, "more than 1 tag"

        k2.info = "Edited Info 2"
        k2.setTags(["edited2"])
        self.kdao.conn.commit()
        # k2 should NOT be edited now
        k2x = self.kdao.getKnowteById(2)
        print k2x.info
        assert k2x.info != "Edited Info 2", "k2 info not edited after just commiting"
        assert len(k2x.taglist()) > 1, "k2 tags not edited after just commiting"
        
        # knowte has been correctly edited
        assert k2.title.startswith("Title for a new"), "title 2 not edited"
        assert k2.info == "Edited Info 2", "info 2 edited"
        assert len(k2.taglist()) == 1, "just 1 tag"
        assert k2.taglist()[0] == "edited2"
        # all other knowtes still have their values
        for k in self.kdao.getKnowtes():
            if k.id != 1 and k.id != 2:
                assert k.title != "Edited Title", "title not edited"
                assert k.info != "Edited Info", "info not edited"
                assert len(k.taglist()) > 1, "more than 1 tag"



if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testEditKnowte']
    unittest.main()